using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Ltf.Remoting.RemoteEvents;
using Ltf.Remoting.Operations;

namespace Ltf.MonitorLizard.Forms
{
	

	public partial class BusyIndicator : Form, BusyForm , IBusyMessageReceiver
	{
		public event EventHandler Cancel;

		private string text;
		private string dots;
		private bool allowCancel;

		private bool disposed = false;

		private System.Timers.Timer timer;

		public BusyIndicator(string title, string mainLabelText,
			 bool allowCancel)
		{
			InitializeComponent();
			this.Text = title;
			lblMainLabel.Text = mainLabelText;
			text = mainLabelText;
			dots = string.Empty;
			this.allowCancel = allowCancel;

			btnCancel.Visible = allowCancel;

			//EventHandler eh = new EventHandler(TimedUpdater);
			//eh.BeginInvoke(this, new EventArgs(), null, null);

			this.Show();
			this.BringToFront();
			this.Activate();
			this.Invalidate();
			this.Refresh();
		}


	//  void TimedUpdater(object sender, EventArgs e)
	//  {
	//    while (!disposed)
	//    {
	//      try
	//      {
	//        this.Invoke(new EventHandler(Update));
	//        System.Threading.Thread.Sleep(200);
	//      }
	//      catch
	//      {
	//      }
	//    }
	//  }


	//  public void Update (object sender, EventArgs e)
	//{
	//    dots += " .";
	//    lblMainLabel.Text = text + dots;
	//    if (dots.Length > 8)
	//      dots = string.Empty;
	//    this.Activate();
	//    this.Invalidate();
	//    this.Refresh();
			

	//  }

		public new void Close()
		{
			this.Invoke(new EventHandler(CloseForm));
		}

		private void CloseForm(object Sender, EventArgs e)
		{
			timer.Enabled = false;
			base.Close();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			cancelButtonPressed = true;
			if (Cancel != null)
				Cancel(this, new EventArgs());
		}

		private bool cancelButtonPressed;

		public bool CancelButtonPressed
		{
			get { return cancelButtonPressed; }
			set { cancelButtonPressed = value; }
		}

		public delegate void SetMessageDelegate(string message);

		public void SetMessageText(string message)
		{
			lblMainLabel.Text = message;
			this.Activate();
			this.Invalidate();
			this.Refresh();
		}

		#region IBusyMessageReceiver Members

		public void SendMessage(string message)
		{
			this.Invoke(new SetMessageDelegate(SetMessageText),message);
		}

		#endregion
	}
}